//
//  WQDeviceRootListViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/18.
//

#import "WQDeviceRootListViewController.h"
#import <ThingSmartBizCore/ThingSmartBizCore.h>

@interface WQDeviceRootListViewController ()

@end

@implementation WQDeviceRootListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreen_Width - kFragmemtFloat(33)) / 2, kFragmemtFloat(150));
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.backgroundColor = UIColor.clearColor;
    
    [self.collectionView registerClass:[WQDeviceStatusCollectionCell class] forCellWithReuseIdentifier:[WQDeviceStatusCollectionCell kj_runtimeClassName]];
    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(10));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(10));
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    
    [self collBeginRefresh];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collBeginRefresh) name:kNotificationRoomDeviceUpdate object:nil];
}

- (UIView *)listView {
    return self.view;
}

#pragma mark ------- Http Mehods -------

- (void)requestHandlerWishIsRefresh:(BOOL)isRefresh {
    
    ThingSmartRoom *room = [[ThingSmartRoom alloc] initWithRoomId:self.roomModel.roomId homeId:WQShare.userHomeModel.homeId];
    
    self.collectionData = room.deviceList;
    [self collEndRefresh];
}

#pragma mark ------- UICollectionViewDelegate -------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionData.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WQDeviceStatusCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[WQDeviceStatusCollectionCell kj_runtimeClassName] forIndexPath:indexPath];
    
    ThingSmartDeviceModel *deviceModel = self.collectionData[indexPath.row];
    [cell updateUIWithModel:deviceModel];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ThingSmartDeviceModel *deviceModel = self.collectionData[indexPath.row];
    
    id<ThingPanelProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingPanelProtocol)];
    [impl gotoPanelViewControllerWithDevice:deviceModel group:nil initialProps:nil contextProps:nil completion:^(NSError * _Nullable error) {
        
    }];
}

@end
